const express = require('express');
const router = express.Router();
const os = require('os');
var osutils = require('os-utils');
var diskinfo = require('diskinfo');
const mysql = require("../../../mysql/db")
const { localStorage } = require("../../../utils/require")
router.get('/', (req, res) => {

    // var dealMem = (mem) => {
    //     var G = 0,
    //         M = 0,
    //         KB = 0;
    //     (mem > (1 << 30)) && (G = (mem / (1 << 30)).toFixed(2));
    //     (mem > (1 << 20)) && (mem < (1 << 30)) && (M = (mem / (1 << 20)).toFixed(2));
    //     (mem > (1 << 10)) && (mem > (1 << 20)) && (KB = (mem / (1 << 10)).toFixed(2));
    //     return G > 0 ? G + 'G' : M > 0 ? M + 'M' : KB > 0 ? KB + 'KB' : mem + 'B';
    //     // return G > 0 ? G : M > 0 ? M : KB > 0 ? KB : mem;
    // };
    var dealMem = (limit) => {
            var size = "";
            if (limit < 0.1 * 1024) { //小于0.1KB，则转化成B
                size = limit.toFixed(2) + "B"
            } else if (limit < 0.1 * 1024 * 1024) { //小于0.1MB，则转化成KB
                size = (limit / 1024).toFixed(2) + "KB"
            } else if (limit < 0.1 * 1024 * 1024 * 1024) { //小于0.1GB，则转化成MB
                size = (limit / (1024 * 1024)).toFixed(2) + "MB"
            } else { //其他转化成GB
                size = (limit / (1024 * 1024 * 1024)).toFixed(2)
            }
            var sizeStr = size + ""; //转成字符串
            var index = sizeStr.indexOf("."); //获取小数点处的索引
            var dou = sizeStr.substr(index + 1, 2) //获取小数点后两位的值
            if (dou == "00") { //判断后两位是否为00，如果是则删除00
                return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
            }
            return size;
        }
        //内存
    const totalMem = os.totalmem();
    const freeMem = os.freemem();



    //获得所有磁盘空间

    //当前盘符
    var current_disk = __dirname.substr(0, 2).toLowerCase();
    let a = __dirname
    diskinfo.getDrives((err, aDrives) => {
        //遍历所有磁盘信息
        let disktotal = 0; //总量
        let diskused = 0; //已使用
        for (let i = 0; i < aDrives.length; i++) {
            if (aDrives[i].mounted.toLowerCase() == current_disk) {
                //总量
                disktotal = (aDrives[i].blocks / 1024 / 1024 / 1024).toFixed(0);
                //已使用
                diskused = (aDrives[i].used / 1024 / 1024 / 1024).toFixed(0);
            }
        }

        //cpu使用率
        const cpus = os.cpus();
        //parseFloat(((1 - times.idle / (times.idle + times.user + times.nice + times.sys + times.irq)) * 100).toFixed(2))

        osutils.cpuUsage((value) => {
            let sql = "SELECT DISTINCT date_format(g.datetime,'%Y-%m-%d') as datetime,ifnull (u.count,0)  as usercount,ifnull(g.count,0) as logcount FROM (SELECT count(1) AS 'count', date_format(datetime, '%Y-%m-%d') AS datetime FROM  `user` WHERE YEARWEEK( date_format( datetime,'%Y-%m-%d'),1 ) = YEARWEEK(now()) GROUP BY date_format(datetime, '%Y-%m-%d')) as u right join (SELECT count(1) AS 'count', date_format(datetime, '%Y-%m-%d') AS datetime FROM  `general_log` WHERE YEARWEEK( date_format( datetime,'%Y-%m-%d'),1 ) = YEARWEEK(now(),1) and reason='登录成功' GROUP BY date_format(datetime, '%Y-%m-%d')) as g on date_format(u.datetime,'%Y-%m-%d') = date_format(g.datetime,'%Y-%m-%d') ORDER BY datetime ";
            mysql.query(sql, (err, doc) => {
                if (err) {
                    throw err;
                }
                // console.log(123, doc)
                res.send({
                    sesstion: req.session.users,
                    memory: { totalMem: parseFloat(dealMem(totalMem)), freeMem: parseFloat((dealMem(totalMem)) - dealMem(freeMem)).toFixed(2) },
                    cpu: { core: cpus.length, rate: parseFloat((value * 100).toFixed(2)) },
                    echarts: doc,
                    disk: { disktotal: parseFloat(disktotal), diskused: parseFloat(diskused) },
                    role_name: localStorage.getItem("role_name")
                });

            })



        });



    });








})


module.exports = router